<?php

function eveonline_tactical_map_page() {
  $output = "<img src=\"http://hellriders-eve.ru/eveonline/tacticalmap/image\">";
  $output .= drupal_get_form('eveonline_tactical_map_bridge_form');
  return $output;
}

function eveonline_tactical_map_bridge_form($form_state) {
  $form['bridge'] = array(
    '#type' => 'fieldset',
    '#title' => t('Add bridge'),
    '#collapsible' => FALSE,
  );
  $form['bridge']['fromSystem'] = array(
    '#title' => t('From'),
    '#type' => 'textfield',
    '#size' => 110,
    '#default_value' => '',
    '#autocomplete_path' => 'eveonline/autocomplete/bridges'
  );
  $form['bridge']['fromPlanet'] = array(
    '#type' => 'textfield',
    '#title' => t('Planet'),
    '#size' => 1,
    '#maxlength' => 3,
    '#default_value' => '1',
  );
  $form['bridge']['fromMoon'] = array(
    '#type' => 'textfield',
    '#title' => t('Moon'),
    '#size' => 1,
    '#maxlength' => 3,
    '#default_value' => '1',
  );
  $form['bridge']['toSystem'] = array(
    '#title' => t('To'),
    '#type' => 'textfield',
    '#size' => 110,
    '#default_value' => '',
    '#autocomplete_path' => 'eveonline/autocomplete/bridges'
  );
  $form['bridge']['toPlanet'] = array(
    '#type' => 'textfield',
    '#title' => t('Planet'),
    '#size' => 1,
    '#maxlength' => 3,
    '#default_value' => '1',
  );
  $form['bridge']['toMoon'] = array(
    '#type' => 'textfield',
    '#title' => t('Moon'),
    '#size' => 1,
    '#maxlength' => 3,
    '#default_value' => '1',
  );
  $form['bridge']['pass'] = array(
    '#type' => 'textfield',
    '#title' => t('Password'),
    '#size' => 20,
    '#maxlength' => 20,
    '#default_value' => '',
  );
  $form['bridge']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Add'),
  );
  return $form;
}

function eveonline_tactical_map_bridge_form_submit($form, &$form_state) {
  $data = (object) $form_state['values'];
  $fromSystemID = db_fetch_object(db_query("SELECT solarSystemID FROM eve.mapSolarSystems WHERE solarSystemName	LIKE '%s'", $data->fromSystem))->solarSystemID;
  $toSystemID = db_fetch_object(db_query("SELECT solarSystemID FROM eve.mapSolarSystems WHERE solarSystemName	LIKE '%s'", $data->toSystem))->solarSystemID;
  $savedata = array(
    'fromSystemID' => $fromSystemID,
    'fromPlanet' => $data->fromPlanet,
    'fromMoon' => $data->fromMoon,
    'toSystemID' => $toSystemID,
    'toPlanet' => $data->toPlanet,
    'toMoon' => $data->toMoon,
    'pass' => $data->pass,
  );
  drupal_write_record('eveonline_tacticalmap_bridges', $savedata);
  drupal_set_message(t('Bridge added'));
  $form_state['redirect'] = 'eveonline/tacticalmap';
}

function eveonline_tactical_map_image($size = 700) {
  db_set_active('eve');
  $sql = db_query("SELECT *
    FROM
      mapSolarSystems
    WHERE
      regionID = 10000059
      OR regionID = 10000056
      OR regionID = 10000031
      OR regionID = 10000014
      OR regionID = 10000039
  ");
  while ($row = db_fetch_object($sql)) {
    $solarSystem[$row->solarSystemID] = $row;
    $maxX = $row->x;
    $minX = $row->x;
    $maxZ = $row->z;
    $minZ = $row->z;
  }

  $sql = db_query("SELECT *
    FROM
      mapSolarSystemJumps
    WHERE
      fromRegionID = 10000059
      OR fromRegionID = 10000056
      OR fromRegionID = 10000031
      OR fromRegionID = 10000014
      OR fromRegionID = 10000039
  ");
  while ($row = db_fetch_object($sql)) {
    if (!isset($jumps[$row->toSolarSystemID][$row->fromSolarSystemID])) {
      if ($row->fromRegionID <> $row->toRegionID) {
        $jumps[$row->fromSolarSystemID][$row->toSolarSystemID] = 2;
      }
      elseif ($row->fromConstellationID <> $row->toConstellationID) {
        $jumps[$row->fromSolarSystemID][$row->toSolarSystemID] = 1;
      }
      else {
        $jumps[$row->fromSolarSystemID][$row->toSolarSystemID] = 0;
      }
    }
  }

  db_set_active('default');

  $sql = db_query("SELECT * FROM {eveonline_tacticalmap_bridges}");
  while ($row = db_fetch_object($sql)) {
    if (!isset($jb[$row->toSystemID])) {
      $jb[$row->fromSystemID] = $row->toSystemID;
      $jbpm[$row->fromSystemID] = $row->fromPlanet . '-' . $row->fromMoon;
      $jbpm[$row->toSystemID] = $row->toPlanet . '-' . $row->toMoon;
    }
  }

  foreach ($solarSystem as $item) {
    if ($item->x > $maxX) {
      $maxX = $item->x;
    }
    if ($item->x < $minX) {
      $minX = $item->x;
    }
    if ($item->z > $maxZ) {
      $maxZ = $item->z;
    }
    if ($item->z < $minZ) {
      $minZ = $item->z;
    }
  }
  $width = $maxX - $minX;
  $height = $maxZ - $minZ;
  $scale = $width / $size;
  $iwidth = round($width / $scale);
  $iheight = round($height / $scale);
  drupal_set_header('Content-type: image/svg+xml');

  echo '<?xml version="1.0" encoding="UTF-8" standalone="no"?>';
  echo '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">';
  echo '<svg width="' . $iwidth . '" height="' . $iheight . '" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1">';
  echo '<title>Tactical map</title>';
  echo '<desc> Jumpbridges, cynogens and etc </desc>';

  echo '<defs>';
  foreach ($jb as $from => $to) {
    $x1 = round((($solarSystem[$from]->x - $minX) / $scale));
    $x2 = round((($solarSystem[$to]->x - $minX) / $scale));
    $y1 = round((($solarSystem[$from]->z - $minZ) / $scale));
    $y2 = round((($solarSystem[$to]->z - $minZ) / $scale));
    eveonline_pathbridge($x1, $y1, $x2, $y2, $solarSystem[$from]->solarSystemName);
    eveonline_pathbridge($x1, $y1, $x2, $y2, $solarSystem[$to]->solarSystemName);
  }
  echo '</defs>';

  foreach ($solarSystem as $item) {
    $x = round(($item->x - $minX) / $scale);
    $y = round(($item->z - $minZ) / $scale);
    if (isset($jumps[$item->solarSystemID])) {
      foreach ($jumps[$item->solarSystemID] as $jump => $type) {
        if (isset($solarSystem[$jump])) {
          switch ($type) {
            case 0: default:
              $color = 'Gray';
              break;
            case 1:
              $color = 'DarkBlue';
              break;
            case 2:
              $color = 'DarkViolet';
              break;
          }
          $x2 = round(($solarSystem[$jump]->x - $minX) / $scale);
          $y2 = round(($solarSystem[$jump]->z - $minZ) / $scale);
          echo '<line x1="' . $x . '" y1="' . $y . '" x2="' . $x2 . '" y2="' . $y2 . '" stroke="' . $color . '" stroke-width="1"/>';
        }
      }
    }
    if (isset($jbpm[$item->solarSystemID])) {
      echo '<circle cx="' . $x . '" cy="' . $y . '"  r="5" fill="Red"/>';
    }
    else {
      echo '<circle cx="' . $x . '" cy="' . $y . '"  r="3" fill="Gray"/>';
    }
  }
  foreach ($jb as $from => $to) {
    $x1 = round((($solarSystem[$from]->x - $minX) / $scale));
    $x2 = round((($solarSystem[$to]->x - $minX) / $scale));
    $y1 = round((($solarSystem[$from]->z - $minZ) / $scale));
    $y2 = round((($solarSystem[$to]->z - $minZ) / $scale));
    eveonline_drawbridge($x1, $y1, $x2, $y2, 'Red');
    if ($x1 > $x2) {
      echo '<text font-size="12" font-family="Arial" font-weight="bold" fill="Blue">';
      echo '<textPath xlink:href="#' . $solarSystem[$to]->solarSystemName . '">';
      echo '<tspan dy="-5">' . $solarSystem[$to]->solarSystemName . ' (' . $jbpm[$to] . ')</tspan>';
      echo '</textPath></text>';

      echo '<text font-size="12" font-family="Arial" font-weight="bold" text-anchor="end" fill="Blue">';
      echo '<textPath xlink:href="#' . $solarSystem[$from]->solarSystemName . '" startOffset="100%">';
      echo '<tspan dy="-5">' . $solarSystem[$from]->solarSystemName . ' (' . $jbpm[$from] . ')</tspan>';
      echo '</textPath></text>';
    }
    else {
      echo '<text font-size="12" font-family="Arial" font-weight="bold" fill="Blue">';
      echo '<textPath xlink:href="#' . $solarSystem[$from]->solarSystemName . '">';
      echo '<tspan dy="-5">' . $solarSystem[$from]->solarSystemName . ' (' . $jbpm[$from] . ')</tspan>';
      echo '</textPath></text>';

      echo '<text font-size="14" font-family="Arial" font-weight="bold" text-anchor="end" fill="Blue">';
      echo '<textPath xlink:href="#' . $solarSystem[$to]->solarSystemName . '" startOffset="100%">';
      echo '<tspan dy="-5">' . $solarSystem[$to]->solarSystemName . ' (' . $jbpm[$to] . ')</tspan>';
      echo '</textPath></text>';
    }
  }
  echo '</svg>';
  exit();
}

function eveonline_drawbridge($x1, $y1, $x2, $y2, $color) {
  if ($x1 > $x2) {
    if ($y1 > $y2) {
      $z = sqrt(($x1 - $x2) * ($x1 - $x2) + ($y1 - $y2) * ($y1 - $y2));
      $a = rad2deg(atan(($y1 - $y2) / ($x1 - $x2)));
      $sx = round(($z / 2) * cos(deg2rad(45 + $a)));
      $sy = round(($z / 2) * sin(deg2rad(45 + $a)));
      $cx1 = $x1 - $sx;
      $cy1 = $y1 - $sy;
      $cx2 = $x2 + $sx;
      $cy2 = $y2 + $sy;
      echo '<path d="M' . $x1 . ',' . $y1 . ' C' . $cx1 . ',' . $cy1 . ' ' . $cx2 . ',' . $cy2 . ' ' . $x2 . ',' . $y2 . '" style="fill:none; stroke:' . $color . '; stroke-width:4"/>';
    }
    elseif ($y1 < $y2) {
      $z = sqrt(($x1 - $x2) * ($x1 - $x2) + ($y2 - $y1) * ($y2 - $y1));
      $a = rad2deg(atan(($y2 - $y1) / ($x1 - $x2)));
      $sx = round(($z / 2) * cos(deg2rad(45 - $a)));
      $sy = round(($z / 2) * sin(deg2rad(45 - $a)));
      $cx1 = $x1 - $sx;
      $cy1 = $y1 - $sy;
      $cx2 = $x2 + $sx;
      $cy2 = $y2 + $sy;
      echo '<path d="M' . $x1 . ',' . $y1 . ' C' . $cx1 . ',' . $cy1 . ' ' . $cx2 . ',' . $cy2 . ' ' . $x2 . ',' . $y2 . '" style="fill:none; stroke:' . $color . '; stroke-width:4"/>';
    }
  }
  elseif ($x1 < $x2) {
    if ($y1 > $y2) {
      $z = sqrt((($x2 - $x1) * ($x2 - $x1)) + (($y1 - $y2) * ($y1 - $y2)));
      $a = rad2deg(atan(($y1 - $y2) / ($x2 - $x1)));
      $sx = round(($z / 2) * cos(deg2rad(135 - $a)));
      $sy = round(($z / 2) * sin(deg2rad(135 - $a)));
      $cx1 = $x1 - $sx;
      $cy1 = $y1 - $sy;
      $cx2 = $x2 + $sx;
      $cy2 = $y2 + $sy;
      echo '<path d="M' . $x1 . ',' . $y1 . ' C' . $cx1 . ',' . $cy1 . ' ' . $cx2 . ',' . $cy2 . ' ' . $x2 . ',' . $y2 . '" style="fill:none; stroke:' . $color . '; stroke-width:4"/>';
    }
    elseif ($y1 < $y2) {
      $z = sqrt(($x2 - $x1) * ($x2 - $x1) + ($y2 - $y1) * ($y2 - $y1));
      $a = rad2deg(atan(($y2 - $y1) / ($x2 - $x1)));
      $sx = round(($z / 2) * cos(deg2rad(45 - $a)));
      $sy = round(($z / 2) * sin(deg2rad(45 - $a)));
      $cx1 = $x1 + $sx;
      $cy1 = $y1 - $sy;
      $cx2 = $x2 - $sx;
      $cy2 = $y2 + $sy;
      echo '<path d="M' . $x1 . ',' . $y1 . ' C' . $cx1 . ',' . $cy1 . ' ' . $cx2 . ',' . $cy2 . ' ' . $x2 . ',' . $y2 . '" style="fill:none; stroke:' . $color . '; stroke-width:4"/>';
    }
  }
}

function eveonline_pathbridge($x1, $y1, $x2, $y2, $id) {
  if ($x1 > $x2) {
    if ($y1 > $y2) {
      $z = sqrt(($x1 - $x2) * ($x1 - $x2) + ($y1 - $y2) * ($y1 - $y2));
      $a = rad2deg(atan(($y1 - $y2) / ($x1 - $x2)));
      $sx = round(($z / 2) * cos(deg2rad(45 + $a)));
      $sy = round(($z / 2) * sin(deg2rad(45 + $a)));
      $cx1 = $x1 - $sx;
      $cy1 = $y1 - $sy;
      $cx2 = $x2 + $sx;
      $cy2 = $y2 + $sy;
      echo '<path id="' . $id . '" d="M' . $x2 . ',' . $y2 . ' C' . $cx2 . ',' . $cy2 . ' ' . $cx1 . ',' . $cy1 . ' ' . $x1 . ',' . $y1 . '"/>';
    }
    elseif ($y1 < $y2) {
      $z = sqrt(($x1 - $x2) * ($x1 - $x2) + ($y2 - $y1) * ($y2 - $y1));
      $a = rad2deg(atan(($y2 - $y1) / ($x1 - $x2)));
      $sx = round(($z / 2) * cos(deg2rad(45 - $a)));
      $sy = round(($z / 2) * sin(deg2rad(45 - $a)));
      $cx1 = $x1 - $sx;
      $cy1 = $y1 - $sy;
      $cx2 = $x2 + $sx;
      $cy2 = $y2 + $sy;
      echo '<path id="' . $id . '" d="M' . $x2 . ',' . $y2 . ' C' . $cx2 . ',' . $cy2 . ' ' . $cx1 . ',' . $cy1 . ' ' . $x1 . ',' . $y1 . '"/>';
    }
  }
  elseif ($x1 < $x2) {
    if ($y1 > $y2) {
      $z = sqrt((($x2 - $x1) * ($x2 - $x1)) + (($y1 - $y2) * ($y1 - $y2)));
      $a = rad2deg(atan(($y1 - $y2) / ($x2 - $x1)));
      $sx = round(($z / 2) * cos(deg2rad(135 - $a)));
      $sy = round(($z / 2) * sin(deg2rad(135 - $a)));
      $cx1 = $x1 - $sx;
      $cy1 = $y1 - $sy;
      $cx2 = $x2 + $sx;
      $cy2 = $y2 + $sy;
      echo '<path id="' . $id . '" d="M' . $x1 . ',' . $y1 . ' C' . $cx1 . ',' . $cy1 . ' ' . $cx2 . ',' . $cy2 . ' ' . $x2 . ',' . $y2 . '"/>';
    }
    elseif ($y1 < $y2) {
      $z = sqrt(($x2 - $x1) * ($x2 - $x1) + ($y2 - $y1) * ($y2 - $y1));
      $a = rad2deg(atan(($y2 - $y1) / ($x2 - $x1)));
      $sx = round(($z / 2) * cos(deg2rad(45 - $a)));
      $sy = round(($z / 2) * sin(deg2rad(45 - $a)));
      $cx1 = $x1 + $sx;
      $cy1 = $y1 - $sy;
      $cx2 = $x2 - $sx;
      $cy2 = $y2 + $sy;
      echo '<path id="' . $id . '" d="M' . $x1 . ',' . $y1 . ' C' . $cx1 . ',' . $cy1 . ' ' . $cx2 . ',' . $cy2 . ' ' . $x2 . ',' . $y2 . '"/>';
    }
  }
}

function eveonline_bridges_autocomplete($string = '') {
  $matches = array();
  if ($string != '') {
    db_set_active('eve');
    $result = db_query_range(db_rewrite_sql("
          SELECT solarSystemName, solarSystemID
          FROM
            mapSolarSystems
          WHERE
            (regionID = 10000059
            OR regionID = 10000056
            OR regionID = 10000031
            OR regionID = 10000014
            OR regionID = 10000039) AND (solarSystemName	LIKE '%s%%')
          ", 'cl', 'className'), $string, 0, 15);

    while ($system = db_fetch_object($result)) {
      $matches[$system->solarSystemName] = check_plain($system->solarSystemName);
    }
    db_set_active('default');
  }
  drupal_json($matches);
}

?>